home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0387.arc / IMAGEIO.C < prev    next >
C/C++ Source or Header  |  1985-07-12  |  7KB  |  196 lines

  1.  
  2. /*
  3.  *              imageio.c
  4.  *
  5.  * These routines provide the base level image I/O routines. There is a
  6.  * readimage() and writeimage() routine. Both require only a pointer to
  7.  * a memory array and a pointer to a file name.
  8.  *
  9.  */
  10.  
  11. #include <exec/types.h>         /* The UBYTE and USHORT types are here  */
  12. #include <stdio.h>              /* The standard C I/O functions         */
  13. #include <fcntl.h>              /* The Level 1 file I/O constants       */
  14.  
  15. /*
  16.  * Function : SetPixel
  17.  * This function will set a pixel in the image to the given value. 
  18.  * It is passed four values; the first is a pointer to the 
  19.  * image array, the second through fourth are the row, column and value 
  20.  * of the pixel, which are all integers.
  21.  */
  22.  
  23. void
  24. SetPixel(image,col,row,val)
  25.  
  26. UBYTE   image[];        /* An array of pixels                           */
  27. int     col,            /* The pixel x coordinate or column            */
  28.         row,            /* The pixel y coordinate or row               */
  29.         val;            /* a value between 0 and 15 for the pixel       */
  30.  
  31. {
  32.   int   temp,           /* a temporary value                              */
  33.         index,          /* The index into the array                       */
  34.         shift;          /* shift factor (4 if pixel even, 0 if it is odd) */
  35.   
  36.   /* Since two pixels are contained in a byte the 640 pixel line is really
  37.    * 320 bytes wide, and the column value divided by two is the byte containing
  38.    * the pixel we want. If column is even the pixel is in the left half of the 
  39.    * byte and if column is odd the pixel is in the right half.
  40.    */
  41.  
  42.   shift = (col % 2) ? 0 : 4 ;
  43.   index = (row * 320) + (col / 2);
  44.   temp = val << shift;
  45.   if ((index < 0) || (index > 127999)) /* Index checking */
  46.     printf("Error! Bad row and column passed to SetPixel.\n");
  47.   else 
  48.     image[index] = (image[index] & (0x0f0 >> shift)) + temp;
  49. }
  50.  
  51. /*
  52.  * Function : Pixel
  53.  * This function will return the value of the pixel in the image. It is
  54.  * passed four values; the first is a pointer to the image array, the 
  55.  * second through fourth are the row, column and value of the pixel, which 
  56.  * are all integers.
  57.  * 
  58.  */
  59.  
  60. int
  61. Pixel(image,col,row)
  62.  
  63. UBYTE   image[];        /* An array of pixels                           */
  64. int     col,            /* The pixel x coordinate or column            */
  65.         row;            /* The pixel y coordinate or row               */
  66.  
  67. {
  68.   int   temp,           /* a temporary value                              */
  69.         index,          /* The index into the array                       */
  70.         shift;          /* shift factor (4 if pixel even, 0 if it is odd) */
  71.   
  72.   /* This is the same calculation as in SetPixel above */
  73.  
  74.   temp = 0;
  75.   shift = (col % 2) ? 0 : 4 ;
  76.   index = (row * 320) + (col / 2);
  77.   if ((index < 0) || (index > 127999)) /* Index checking */
  78.     printf("Error! Illegal values passed to Pixel(%d,%d).\n",col,row);
  79.   else 
  80.     temp = (image[index] >> shift) & 0x0f;
  81.   return(temp);
  82. }
  83.  
  84. /*
  85.  * Function : ReadImage 
  86.  * This function will read in the 4 bitplanes from the file specified and 
  87.  * store them as 4 bit pixels in the image array. It returns zero if it
  88.  * was successful, and a negative number if it detected an error. 
  89.  * Errors include :
  90.  *      -1      Couldn't open 'filename'
  91.  *      -2      Didn't read enough pixel data.
  92.  *      -3      Didn't read enough colormap data.
  93.  */
  94.  
  95. int
  96. ReadImage(filename, image, colormap)
  97.  
  98. char    *filename;      /* A pointer to a filename string       */
  99. UBYTE   *image;         /* A pointer to a 128000 byte array     */
  100. USHORT  *colormap;      /* An array of color map entries        */
  101.  
  102. {
  103.   short i,j,k;  
  104.   int   n,fh,           /* Byte count, File Handle                        */
  105.         error,          /* indicator that an error occurred during read   */
  106.         ishft,pshft;    /* Some shift factors for manipulating bits.      */  
  107.   UBYTE pixels[80];     /* 640 bits worth (one line) of pixel data.       */
  108.  
  109.   /* Open the input file */
  110.  
  111.   fh = open(filename,O_RDONLY);
  112.   if (fh == -1) return(-1); /* Return error if it couldn't be opened */
  113.   error = 0; 
  114.  
  115.   printf("Reading in source image from file %s ... ",filename);
  116.   for (i=0; i<4; i++) { /* Four bit planes */
  117.     for (k=0; k<400; k++) { /* 400 lines */
  118.       n = read(fh,pixels,80); /* Read in a row of pixels */
  119.       if (n == 80) {
  120.         for (j=0; j<640; j++) {        /* Unpack the pixels             */
  121.           ishft = (j % 2) ? i+4 : i;   /* the image's byte shift factor */
  122.           pshft = (7-j%8);             /* The pixel's byte shift factor */
  123.           *(image+(j/2)+(k*320)) &= ~(1 << ishft); /* Clear old bit     */
  124.           *(image+(j/2)+(k*320)) |= ((pixels[j>>3] >> pshft) & 1) << ishft;
  125.         }
  126.       }
  127.       else error = -1; /* If we didn't get enough data it is an error */
  128.     }
  129.   }
  130.   if (error == 0) {
  131.     j = read(fh,(UBYTE *)colormap,32); /* Read in the color map */
  132.     if (j != 32) error = -1;
  133.   }
  134.   printf("Done.\n");
  135.   close(fh);
  136.   return(error);
  137. }
  138.  
  139. /*
  140.  * Function : WriteImage
  141.  * This function will write out the image array to the specified file. It
  142.  * converts the 4 bit/pixel format of the image array to the bit plane 
  143.  * format used by the iff conversion programs. It returns zero if it was
  144.  * successful and a negative number if it detected an error. 
  145.  * Errors include :
  146.  *      -1      Couldn't open file
  147.  *      -2      Unexpected EOF (probably the disk was full)
  148.  *      -3      Unexpected EOF while writing colormap 
  149.  */
  150.  
  151. int
  152. WriteImage(filename, image, colormap)
  153.  
  154. char    *filename;      /* A pointer to a filename string       */
  155. UBYTE   *image;         /* A pointer to a 128000 byte array     */
  156. USHORT  colormap[];     /* An array of color map entries        */
  157.  
  158. {
  159.   short i,j,k; 
  160.   int   n,fh,           /* byte count, File Handle                        */
  161.         error,          /* indicator that an error occurred during read   */
  162.         ishft,pshft;    /* Some shift factors for manipulating bits.      */  
  163.   UBYTE pixels[80];     /* 640 bits worth (one line) of pixel data.       */
  164.  
  165.   /* First we open the file */
  166.   fh = open(filename,O_WRONLY+O_CREAT); /* Write only, and create it */
  167.   if (fh == -1) return(-1);
  168.   error = 0;
  169.  
  170.   /* Now write out the image */
  171.  
  172.   printf("Writing the processed image to file %s ... ",filename);
  173.   for (i=0; i<4; i++) {              /* Four bit planes                    */
  174.     for (k=0; k<400; k++) {          /* 400 lines                          */
  175.       for (j=0; j<640; j++) {        /* Unpack the pixels                  */
  176.         ishft = (j % 2) ? 4+i : i;   /* The bit number in the image byte   */
  177.         pshft = 7 - (j % 8);         /* Pixel shift value for pixels array */
  178.         pixels[j>>3] &= ~(1 << pshft);    /* clear previous bit            */
  179.         pixels[j>>3] |= ((*(image+(j/2)+(k*320)) >> ishft) & 1) << pshft;
  180.       }
  181.       n = write(fh,pixels,80);  /* Write out this line of bits */
  182.       if (n < 80) error = -2;
  183.     }
  184.   }
  185.   if (error == 0) {
  186.     j = write(fh,(UBYTE *)colormap,32); /* write out the color map */
  187.     if (j < 32) error = -3;
  188.   }
  189.   printf("Done.\n");
  190.  
  191.   close(fh);            /* Clean up after ourselves */
  192.   return(error);        /* And return */
  193.  
  194. }
  195.  
  196.